/**
 * @file Patient.java
 * the description of a Patient as relevant to this project
 * 
 * @author Christopher Nostand (chn8sh)
 * @date UNKNOWN		(created)
 * @date 02 April 2011 (last updated)
 */
package aperturescience.types;

import java.io.IOException;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;

public class Action extends JSONObject implements CouchDBObject {
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final char PREFIX = 'a';
	/*EXAMPLE Action
	 * name = Tylenol
	 * array = ([ (headache, minor_headache), (minor_headache, remove), (create, upset_stomach) ])
	 */

	// CONSTRUCTORS
	
	public Action() {
		super();
	}
	
	public Action(String name)  throws JSONException{
		this();
		super.put(NAME, name);
	}
	
	public Action(String name, String description) throws JSONException {
		this(name);
		super.put(DESCRIPTION, description);
	}
	
	public Action(JsonRepresentation jsonRepresentation) 
	throws JSONException {
		this();
		JSONObject strip = jsonRepresentation.getJsonObject();
		for (String key : JSONObject.getNames(strip))
			super.put(key, strip.get(key));
	}
	
	public Action(Representation representation)
	throws JSONException, IOException {
		this(new JsonRepresentation(representation));
	}
	
	// ACCESSOR METHODS
	
	public String getName() {
		try {
			return (String) super.get(NAME);
		}
		catch (JSONException e) { return null; }
	}

	public boolean setName(String name) {
		try {
			super.remove(NAME);
			super.put(NAME, name);
			return true;
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
	}

	public String getDescription() {
		try {
			return super.getString(DESCRIPTION);
		}
		catch (JSONException e)			{ e.printStackTrace(); }
		catch (NumberFormatException e)	{ e.printStackTrace(); }
		return null;
	}
	
	public boolean setDescription(String desc) {
		try {
			super.remove(DESCRIPTION);
			super.put(DESCRIPTION, desc);
			return true;
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}
	}

	public String getID() {
		try {
			return super.getString(ID);
		}
		catch (JSONException e) { return null; }
	}
	
	public boolean setID(String dbCounter) {
		try {
			super.put(ID, dbCounter);
			return true;
		} catch (JSONException e) {
			return false;
		}
	}

	public String getRevision() {
		try {
			return super.getString(REV);
		}
		catch (JSONException e) { e.printStackTrace(); }
		return null;
	}
	
	
	public boolean setRevision(String newRevision) {
		try {
			super.remove(REV);
			super.put(REV, newRevision);
			return true;
		} catch (JSONException e) { e.printStackTrace(); }
		return false;
	}

	public String getNewState(String oldState){
		try {
			return super.getString(oldState);
		} catch (JSONException e) {
			return oldState;
		}
	}
	
	public PatientState getNewStates(JSONArray oldStates) {
		for (int j=0; j<oldStates.length(); j++) try {
			oldStates.put( j, super.getString( oldStates.getString(j) ) );
		} catch (JSONException e) { continue; }
		return new PatientState(oldStates);
	}
	
	public boolean addTransition(String oldState, String newState) {
		try {
			super.put(oldState, newState);
			return true;
		} catch (JSONException e) {
			return false;
		}
	}
	
	public boolean removeTransition(String oldState) {
		super.remove(oldState);
		return true;
	}
	
	public JSONObject getJSONObject() {
		return (JSONObject)this;
	}
	
	public JsonRepresentation getJsonRepresentation() {
		return new JsonRepresentation(this);
	}

	public char getIdPrefix() {
		return PREFIX;
	}

	@SuppressWarnings("unchecked")
	public Iterator<String> keyIter() {
		return super.keys();
	}
}
